/**
 * validate js
 * @author: wdb
 * @date: 2014.11.05
 * @info: 验证模块
 *
 **/

define(function($) {

    var validate = {
        init : function() {

        },
        verify : function(type,value,value2) {
            switch ( type ) {
                case 'email':
                    return {
                        ok: !!(/[\w\d_\-\.]+@[\w\d_\-\.]+\.[\w]+/ig.test(value)),
                        msg: '邮箱地址不对'
                    };
                    break;

                case 'illegal':
                    return {
                        ok: !(/[\s%^&#$\/\\]+/ig.test(value)),
                        msg: '字符非法'
                    };

                case 'empty':
                    return {
                        ok: !!(value.replace(/\s*/ig, '').length > 0),
                        msg: '此项为必填项'
                    }

                    break;

                case 'twice':
                    return {
                        ok: !!(value == value2),
                        msg: '两次输入不相等'
                    }
                    break;

                case 'password':
                    return {
                        ok: !!(value.length >= 0),
                        msg: '密码长度至少6位'
                    }
                    break;
                case 'mobile':
                    var reg = /^1\d{10}$/;  /*定义正则表达式,手机号只验证是否为11位数字*/
                    return {
                        ok  : (reg.test(value)),
                        msg : '手机号输入有误,【1开头的11位数字】'
                    }
                    break;
                case 'captcha':
                    var reg = /^[a-zA-Z0-9]{4}$/;/*定义验证码正则表达式,4位数字或字母*/
                    var lengthOK = reg.test(value);

                    //验证输入的验证码
                    var text = "" ;
                    var strCookie=document.cookie;
                    var arrCookie=strCookie.split("; ");
                    for(var i=0;i<arrCookie.length;i++){
                        var arr=arrCookie[i].split("=");
                        if(arr[0]=="captcha"){
                            text = arr[1];
                            break;
                        }
                    }
                    var textOK = (text == value.toLowerCase());

                    return {
                        ok  : (lengthOK && textOK),
                        msg : '验证码有误'
                    }
                default:
                    break;
            }
        }
    }

    return validate;

}, {requires:['node']});